package guiClasses;

/**
 * TCSS 360 - Project
 */

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import objectClasses.User;
import objectClasses.UserInfo;
/**
 *  Panel for entering login information
 *  
 *  @author Abraham Santini
 */

public class LoginPanel extends Observable{
	private JPanel my_panel;
	private final Dimension my_text_size = new Dimension(20, 100); 

	/**
	 * public constructor for creating a login panel
	 */
	public LoginPanel(final Observer the_o_er) {
		my_panel = new JPanel();
		addObserver(the_o_er);
		my_panel.setLayout(new GridBagLayout());
		my_panel.add(new JLabel("UserName"), GBC.GBCGen(0, 0, 1, 0));
		final JTextField nameField = new JTextField();
		nameField.setPreferredSize(new Dimension( 100, 20));
		my_panel.add(nameField, GBC.GBCGen(0, 0, 2, 0));

		my_panel.add(new JLabel("Password"), GBC.GBCGen(0,0,1,1));
		final JPasswordField passWordField = new JPasswordField();
		passWordField.setPreferredSize(new Dimension( 100, 20));
		my_panel.add(passWordField, GBC.GBCGen(0, 0, 2, 1));




		//create the login button
		JButton loginButton = new JButton("Login");
		// creates the actionListener for the Login button
		loginButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StringBuilder sb = new StringBuilder();
				char tempArray[] = passWordField.getPassword();
				for(int i = 0; i < tempArray.length; i++) {
					sb.append(tempArray[i]);
				}
				System.out.println(sb.toString());

				UserInfo temp = User.login(nameField.getText(), sb.toString());
				if (temp != null) {
					GuiMain.setUser(temp);
					setChanged();
					switch(temp.getUserType()) {
					case 'A' :
						notifyObservers(new AttendeePanel(the_o_er).getPanel());
						break;
					case 'O' :
						notifyObservers(new OrganizerPanel(the_o_er).getPanel());
						break;
					case 'J' :
						notifyObservers(new JudgePanel(the_o_er).getPanel());
						break;
					case 'C' :
						notifyObservers(new AttendeePanel(the_o_er).getPanel());
						break;
					}
				} else {
					String message ="Login information is incorrect. \n Please try again.";
					JOptionPane.showMessageDialog(my_panel, (Object) message);
				}	
			}			
		});
		GridBagConstraints c = GBC.GBCGen(0,0,1,2);
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.CENTER;
		my_panel.add(loginButton, c);	



		my_panel.add(new JLabel("Need to Register?"), GBC.GBCGen(0,0,1,3));
		JButton registerButton = new JButton("Register");

		// creates the action listener for the register button
		registerButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setChanged();
				notifyObservers(new RegistrationPanel(the_o_er).getPanel());

			}

		});

		my_panel.add(registerButton, GBC.GBCGen(0,0,2,3));



	}// end Login Panel Constructor
	/**
	 * Return the panel.
	 * @return my_panel The panel that contains the login information
	 */
	public JPanel getPanel() {
		return my_panel;
	}

}
